﻿using OpenCvSharp;
using System.Windows;
using System.Windows.Media;

namespace YOLODemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var mat = LoadImageFromFile("apple_1.png");
            ImageCanvas.UpdateWImage(mat.Data, (int)GetMatDataSize(mat), GetMatImageType(mat), mat.Width, mat.Height);
        }
        public static Mat LoadImageFromFile(string path)
        {
            return Cv2.ImRead(path, ImreadModes.AnyColor);
        }
        public static PixelFormat GetMatImageType(Mat image)
        {
            var typOri = image.Type();
            var ret = PixelFormats.Default;

            if (typOri == MatType.CV_8UC1)
            {
                ret = PixelFormats.Gray8;
            }
            else if (typOri == MatType.CV_8UC3)
            {
                ret = PixelFormats.Bgr24;
            }

            return ret;
        }
        public static long GetMatDataSize(Mat image)
        {
            return image.Total() * image.Channels();
        }
    }
}
